/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;


import com.sun.xml.fastinfoset.tools.StAX2SAXReader;
import domain.Orders;
import domain.Passport;
import domain.Users;
import domain.Visa;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.ToggleEvent;
import services.impl.PassportServices;
import services.impl.UserServiceImpl;
import services.impl.VisaSrvices;

/**
 *
 * @author amega
 */
@ManagedBean(name="userBean")
@SessionScoped
public class UserBean {

    public UserBean(){}
    
    @EJB
    private UserServiceImpl userService;
    
    private Users currentUser;
    
    private Users newUser = new Users();
    
    private String email;
    
    private String password;

    private Orders selectUserOrder;
    
    public List<Users> allUsers;
    
    public static String category[] = {
                                    "VIP",
                                    "WORK",
                                    "USER",
                                    "ADMIN"
                                };
   public static  String role[] = {
                                    "USER",
                                    "ADMIN"
                                 };

    public Users getNewUser() {
        return newUser;
    }

    public void setNewUser(Users newUser) {
        this.newUser = newUser;
    }

    public Orders getSelectUserOrder() {
        return selectUserOrder;
    }

    public void setSelectUserOrder(Orders selectUserOrder) {
        this.selectUserOrder = selectUserOrder;
    }

    
    
    public String[] getCategory() {
        return category;
    }
    
    public String[] getRole() {
        return role;
    }
    
    public Users getCurrentUser() {
        return currentUser;
    } 

    public void setCurrentUser(Users currentUser) {
        this.currentUser = currentUser;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public void checkUser(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();  
        FacesMessage msg = null;  
        boolean loggedIn = false;  
          
       if (email != null && password !=null ) {      
            this.currentUser  = userService.getUserByEmailAndPassword(email, password);
            if (this.currentUser != null) {
                loggedIn = true;  
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", this.currentUser.getName()+" "+this.currentUser.getPatronymic());
            } else {
                loggedIn = false;  
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");  
            } 
            
        } else {  
            loggedIn = false;  
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");  
        }  
          
        FacesContext.getCurrentInstance().addMessage(null, msg);  
        context.addCallbackParam("loggedIn", loggedIn);  
    }
    
    public List<Users> getAllUsers() {
        allUsers = userService.findAll();
        return allUsers;
    }
    
    public void onEdit(RowEditEvent event) {
        
        Users u = (Users)event.getObject();
        
        userService.edit(u);
          FacesMessage msg = new FacesMessage("Edited", email);  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
    
    public void onCencel(RowEditEvent event){
    }
    
    public void saveUser(){
      
        
        if (userService.getUserByEmail(newUser.getEmail()) == null) {
            userService.create(newUser);
            if (userPassport.getId() == null) {
                userPassport.setIdUser(newUser);
                passportServices.create(userPassport);
            }
            userVisa.setIdPassport(userPassport);
            userVisa.setIdUser(newUser);
            visaSrvices.create(userVisa);
                    this.emailStatus = "";
            this.newUser = new Users();
            this.numberUserPassport = null;
            this.userVisa = new Visa();
            this.userPassport = new Passport();
        } else {
             FacesMessage msg = new FacesMessage("Ошибка, данный емейл уже сущесвует", email);  
  
             FacesContext.getCurrentInstance().addMessage(null, msg);  
        }
        
    }
    
    public String emailStatus;

    public String getEmailStatus() {
        return emailStatus;
    }

    public void setEmailStatus(String emailStatus) {
        this.emailStatus = emailStatus;
    }
    
    
    
    public void checkEmail(){
        if (userService.getUserByEmail(newUser.getEmail()) != null) {
             this.emailStatus = "Данный адрес уже используется";
        } else {
            this.emailStatus = "";
        }
    }
    
    private Users selectUser;

    public Users getSelectUser() {
        return selectUser;
    }

    public void setSelectUser(Users selectUser) {
        this.selectUser = selectUser;
    }
    
    
    
    public void deleteUser() {
        
        userService.remove(selectUser);
    }
    
   
    private Passport userPassport = new Passport();

    @EJB
    private PassportServices passportServices;
    
    public Passport getUserPassport() {
        return userPassport;
    }

    public void setUserPassport(Passport userPassport) {
        this.userPassport = userPassport;
    }
    
     public void handlePassportToggle(ToggleEvent event){
        //FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Fieldset Toggled", "Visibility:" + event.getVisibility());  
  
        //FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
     
    public String numberUserPassport;

    
    public String getNumberUserPassport() {
        return numberUserPassport;
    }

    public void setNumberUserPassport(String numberUserPassport) {
        this.numberUserPassport = numberUserPassport;
    }
    
    
    
    public void findUserPassport(){
        System.out.println("number:"+numberUserPassport); 
        Passport p = passportServices.getPassportByNumber(numberUserPassport);
        if (p!=null) {
            userPassport = p;
        }
    }
     
    private Visa userVisa = new Visa();
    
    @EJB
    private VisaSrvices visaSrvices;

    public Visa getUserVisa() {
        return userVisa;
    }

    public void setUserVisa(Visa userVisa) {
        this.userVisa = userVisa;
    }
    
    
    
}
